package com.dqp.cslggroup.Api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dqp.cslggroup.ConnectSever.SeverIP;
import com.dqp.cslggroup.UI.MyApplication;
import com.dqp.cslggroup.Util.Result;
import com.dqp.cslggroup.Util.SharedPreferencesUtil;
import com.dqp.cslggroup.Util.StringUtil;
import com.dqp.cslggroup.Util.verification;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;

public class comprehensiveTestApi {
    private static Connection.Response response;
    static String ip = SharedPreferencesUtil.getIp(MyApplication.getContext());


    /*
    * @Description: 登录学生管理系统
    * @Param: [sno, password]
    * @return: com.alibaba.fastjson.JSONObject
    * @Author: 董琴平
    * @Date: 2021/9/6
    */
    public static JSONObject login(String sno, String password) {
        try {
            if (ip == null) ip = SeverIP.getDefvalueUrl();
            response = Jsoup.connect(ip + "/cslg/xsgz/login")
                    .data("sno", sno)
                    .data("password", password)
                    .data("vpn", "1")
                    .ignoreContentType(true)
                    .method(Connection.Method.GET)
                    .timeout(60000)
                    .execute();
            if (response.body() == null) return Result.error("响应超时！");
            String json = response.body();
            if (!StringUtil.isJson(json)) return Result.error("服务器出小差了！");
            return JSON.parseObject(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.error("登录异常");
    }


    /*
     * @Description: 综测数据
     * @Param:
     * @return:
     * @Author: 董琴平
     * @Date: 2021/9/6
     */
    public static JSONObject getData(String sno, String identity) {
        try {
            if (ip == null) ip = SeverIP.getDefvalueUrl();
            response = Jsoup.connect(ip + "/cslg/xsgz/getScholarshipDetails")
                    .data(verification.digitalSignature(15))
                    .data("sno", sno)
                    .ignoreContentType(true)
                    .method(Connection.Method.GET)
                    .timeout(60000)
                    .execute();
            if (response.body() == null) return Result.error("响应超时！");
            String json = response.body();
            if (!StringUtil.isJson(json)) return Result.error("服务器出小差了！");
            return JSON.parseObject(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.error("登录异常");
    }

}
